import {createRouter,createWebHistory} from 'vue-router'
import routes from './modules/routes'
import type { RouteRecordRaw,RouteLocationMatched } from 'vue-router'
import useHeader from '@/hooks/layout/useHeader'

const router = createRouter({
  history:createWebHistory(import.meta.env.VITE_BASE_PATH),
  routes:routes as RouteRecordRaw[]
})

const {updatedRouteRecords,routeRecords} = useHeader()
router.beforeEach(()=>{
  routes.forEach(route=>{
    if(route.name==='HomeIndex'){
      routeRecords.value[0]=route as RouteLocationMatched
      return
    }
    if(route.children?.length!=0){
      const children=route.children?.find(route=>route.name==='HomeIndex')
      if(children!=undefined){
        routeRecords.value[0]=children as RouteLocationMatched
        return
      }
    }
  })
})
router.afterEach(()=>{
  updatedRouteRecords()
})

export default router